//
//  GoodCJTViewController.m
//  BreadTravel
//
//  Created by dllo on 15/12/25.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "GoodCJTViewController.h"
#import "CJTGoodModel.h"
#import "CJTGoodTableViewCell.h"
#import "LRJRecSecTraViewController.h"

@interface GoodCJTViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic, retain)UITableView *myTableView;
@property(nonatomic, retain)NSMutableArray *dataArray;

@end

@implementation GoodCJTViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.view.backgroundColor = [UIColor whiteColor];
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];

    // 去掉cell的线
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.myTableView registerClass:[CJTGoodTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    self.dataArray = [NSMutableArray array];
    
    [self getData];
}

- (void)getData
{
    
    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [hub show:YES];
    hub.labelText = @"加载中";
    // 字符串截取字符串
    NSString *url = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/trips/",self.type,self.id];
    
    [AFNetworkTool getUrl:url body:nil result:HXJSON headerFile:nil success:^(id result) {
       
        NSMutableDictionary *bigDic = result;
        NSMutableArray *items = [bigDic objectForKey:@"items"];
        for (NSMutableDictionary *dic in items)
        {
            CJTGoodModel *model = [CJTGoodModel modelWithDictionary:dic];
            NSString *str = [NSString stringWithFormat:@"%@", model.date_complete];

            if ([str isEqualToString:@"(null)"]) {

            }else{
            [self.dataArray addObject:model];
            }
            
        }
        [self.myTableView reloadData];
        [hub hide:YES];
        
    } failure:^(NSError *error) {
        
        
    }];
    
}

// row
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
//    return 100;
}
// cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    CJTGoodTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    CJTGoodModel *bestModel = [self.dataArray objectAtIndex:indexPath.row];

        cell.model = bestModel;
    
    
    
    return cell;
}
// 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 160;
}
//点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    // 取消cell的置灰效果
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    LRJRecSecTraViewController *travelVC = [[LRJRecSecTraViewController alloc]init];
    CJTGoodModel *model = [self.dataArray objectAtIndex:indexPath.row];
    travelVC.id = model.myId;
    

    [self.navigationController pushViewController:travelVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
